﻿using System;
using System.Data;
using System.Collections.Generic;
using libexcel;

namespace libwutong
{
    class MyExcel
    {
        public const int s_idLen = 12;
        public const string s_pathDate = "扫描查询收件/";
        public const string s_fileCombine = "合并.xlsx";

        static public int combineTableDate(bool isPrint)
        {
            string[] files = System.IO.Directory.GetFiles(s_pathDate);
            DataTable tableIds = new DataTable();
            foreach (string file in files)
            {
                if (file.IndexOf(s_fileCombine) >= 0)
                {
                    continue;
                }
                if (isPrint)
                {
                    Console.WriteLine(" " + file);
                }
                DataTable table = ExcelHelper.getTable(file);
                if (tableIds.Columns.Count == 0)
                {
                    for (int i = 0; i < table.Columns.Count; ++i)
                    {
                        tableIds.Columns.Add(table.Columns[i].ToString());
                    }
                }
                for (int k = 0; k < table.Rows.Count; ++k)
                {
                    DataRow row = tableIds.NewRow();
                    for (int i = 0; i < table.Columns.Count; ++i)
                    {
                        row[i] = table.Rows[k][i];
                    }
                    tableIds.Rows.Add(row);
                }
            }
            if (tableIds.Rows.Count == 0)
            {
                return 0;
            }
            ExcelHelper.writeExcel(tableIds, s_pathDate + s_fileCombine);
            if (isPrint)
            {
                Console.WriteLine(" " + s_pathDate + s_fileCombine);
            }
            return tableIds.Rows.Count;
        }


        static public DataTable getTransitsFromRefer(DataTable tableRefer, out DataTable tableTransit2)
        {
            DataTable tableTransit = new DataTable();
            TxtWeight txtWeight = new TxtWeight();
            MyPrice myPrice = new MyPrice();
            // 运单编号 收件员 签收网点 单价 重量 总价 扫描时间
            tableTransit.Columns.Add(MyTxt.IdField);
            tableTransit.Columns.Add(MyTxt.ReceiveStaffField);
            tableTransit.Columns.Add(MyTxt.SignField);
            tableTransit.Columns.Add("单价");
            tableTransit.Columns.Add(MyTxt.WeightField);
            tableTransit.Columns.Add(MyTxt.WeightField + "(整)");
            tableTransit.Columns.Add("总价");
            tableTransit.Columns.Add(MyTxt.ScanTimeField);

            tableTransit2 = new DataTable();
            for (int i = 0; i < tableTransit.Columns.Count; ++i)
            {
                tableTransit2.Columns.Add(tableTransit.Columns[i].ToString());
            }

            int index, temp;
            float temp1, temp2;
            string str;
            for (int k = 0; k < tableRefer.Rows.Count; ++k)
            {
                DataRow row = tableTransit.NewRow();
                row[0] = tableRefer.Rows[k][0];
                row[1] = tableRefer.Rows[k][1];
                row[2] = tableRefer.Rows[k][2];
                // 单价
                str = tableRefer.Rows[k][2].ToString();
                index = myPrice.Index(str);
                if (index < 0)
                {
                    Console.WriteLine(" 目的地无法解析: {0} ({1})", str, row[0].ToString());
                    DataRow row2 = tableTransit2.NewRow();
                    row2[0] = row[0];
                    row2[1] = row[1];
                    row2[2] = row[2];
                    row2[3] = "???/0/0";
                    // 重量
                    temp1 = Convert.ToSingle(tableRefer.Rows[k][3]);
                    temp2 = Convert.ToSingle(tableRefer.Rows[k][4]);
                    temp = (int)Math.Ceiling(Math.Max(temp1, temp2));
                    row2[4] = tableRefer.Rows[k][3].ToString() + "/" + tableRefer.Rows[k][4].ToString();
                    row2[5] = temp;
                    // 总价
                    row2[6] = 0;

                    row2[7] = tableRefer.Rows[k][6];
                    tableTransit2.Rows.Add(row2);
                    continue;
                }

                row[3] = myPrice.Province(index) + "/" + myPrice.Price(index).ToString() + "/" + myPrice.Price2(index).ToString();
                // 重量
                temp1 = Convert.ToSingle(tableRefer.Rows[k][3]);
                temp2 = Convert.ToSingle(tableRefer.Rows[k][4]);
                temp = (int)Math.Ceiling(Math.Max(temp1, temp2));
                row[4] = tableRefer.Rows[k][3].ToString() + "/" + tableRefer.Rows[k][4].ToString();
                row[5] = temp;
                // 总价
                row[6] = Convert.ToInt32(myPrice.Price(index, temp));

                row[7] = tableRefer.Rows[k][6];
                tableTransit.Rows.Add(row);
            }
            return tableTransit;
        }


        static public DataTable getSummaryFromTransit(DataTable tableTransit)
        {
            List<string> names = new List<string>();
            List<int> counts = new List<int>();
            List<int> costs = new List<int>();
            string name;
            int cost;
            int index;
            for (int k = 0; k < tableTransit.Rows.Count; ++k)
            {
                name = tableTransit.Rows[k][1].ToString();
                cost = Convert.ToInt32(tableTransit.Rows[k][6]);
                index = names.IndexOf(name);
                if(index >= 0)
                {
                    ++counts[index];
                    costs[index] += cost;
                }
                else
                {
                    names.Add(name);
                    counts.Add(1);
                    costs.Add(cost);
                }
            }

            TxtPrice txtPrice = new TxtPrice();
            DataTable tableSummary = new DataTable();
            tableSummary.Columns.Add(MyTxt.ReceiveStaffField);
            tableSummary.Columns.Add("运单数");
            tableSummary.Columns.Add("中转费");
            tableSummary.Columns.Add("合计");
            for(int k = 0; k < names.Count; ++k)
            {
                DataRow row = tableSummary.NewRow();
                row[0] = names[k];
                row[1] = counts[k];
                row[2] = costs[k];
                row[3] = counts[k] * txtPrice.ScanCost + costs[k];
                tableSummary.Rows.Add(row);
            }
            return tableSummary;
        }
    }
}
